var zTreeObj;
// opts 可从网站在线制作
var opts = {
	lines : 13, // 花瓣数目
	length : 18, // 花瓣长度
	width : 10, // 花瓣宽度
	radius : 18, // 花瓣距中心半径
	corners : 1, // 花瓣圆滑度 (0-1)
	rotate : 0, // 花瓣旋转角度
	direction : 1, // 花瓣旋转方向 1: 顺时针, -1: 逆时针
	color : '#000000', // 花瓣颜色
	speed : 2, // 花瓣旋转速度
	trail : 60, // 花瓣旋转时的拖影(百分比)
	shadow : false, // 花瓣是否显示阴影
	hwaccel : false, // spinner 是否启用硬件加速及高速旋转
	className : 'spinner', // spinner css 样式名称
	zIndex : 2e9, // spinner的z轴 (默认是2000000000)
	top : "40%", // spinner 相对父容器Top定位 单位 px
	left : "52%"// spinner 相对父容器Left定位 单位 px
};

var spinner = new Spinner(opts);
var spinner1 = new Spinner(opts);

function addHoverDom(treeId, treeNode) {

	var sObj = $("#" + treeNode.tId + "_span");
	// alert("当前节点ID:"+treeNode.id);
	if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0)
		return;
	var addStr = "<span class='glyphicon glyphicon-plus-sign' id='addBtn_"
			+ treeNode.tId
			+ "' title='add node' onfocus='this.blur();'></span>";
	if (0 != treeNode.id) {
		addStr += "<span class='glyphicon glyphicon-edit' id='editBtn_"
				+ treeNode.tId
				+ "' title='delete node' onfocus='this.blur();'></span>";
		addStr += "<span class='glyphicon glyphicon-trash' id='removeBtn_"
				+ treeNode.tId
				+ "' title='remove node' onfocus='this.blur();'></span>";
	}
	if (treeNode.isParent) {
		addStr += "<span class='glyphicon glyphicon-folder-close' id='addFolderBtn_"
				+ treeNode.tId
				+ "' title='add folder' onfocus='this.blur();'></span>";
	}
	sObj.after(addStr);

	var addBtn = $("#addBtn_" + treeNode.tId);
	if (addBtn) {
		addBtn.bind("click", function() {
			var zTreeObj = $.fn.zTree.getZTreeObj("zTree");
			// alert("点击了添加按钮的方法");
			// alert("属性值ID:" + treeNode.id + ":" + treeId + ":" +
			// treeNode.tId);
			var name = "请输入新用例名称";
			var parentId = treeNode.id;
			var isParent = false;
			var caseType = "WEBUI";
			var id = "";
			if (!treeNode.isParent) {
				var parentId = treeNode.getParentNode().id;
			}
			$.ajax({
				type : "POST",
				url : "addNode.action?parentId=" + parentId + "&name=" + name
						+ "&isParent=" + isParent + "&caseType=" + caseType,
				data : "",
				async : false,
				cache : false,
				success : function(msg) {
					id = msg;
				},
				error : function(e) {
					alert("对不起，后台处理发生异常，请联系管理员!");
				}
			});

			var newNode = {
				id : id,
				name : name,
				isParent : isParent
			};
			if (!treeNode.isParent) {
				newNode = zTreeObj.addNodes(treeNode.getParentNode(), newNode);
			} else {
				newNode = zTreeObj.addNodes(treeNode, newNode);
			}
			// alert("新节点信息:" + JSON.stringify(newNode));
			$.each(newNode, function(n, value) {
				// alert(n + ":" + value.tId);
				zTreeObj.editName(zTreeObj.getNodeByTId(value.tId));
			});
		});
	}
	var editBtn = $("#editBtn_" + treeNode.tId);
	if (editBtn) {
		editBtn.bind("click", function() {
			var treeObj = $.fn.zTree.getZTreeObj("zTree");
			treeObj.editName(treeObj.getNodeByTId(treeNode.tId));

		});
	}
	var removeBtn = $("#removeBtn_" + treeNode.tId);
	if (removeBtn) {
		removeBtn.bind("click", function() {

			if (!confirm("确认删除?")) {
				return;
			}
			var treeObj = $.fn.zTree.getZTreeObj("zTree");
			var node = treeObj.getNodeByTId(treeNode.tId);
			var id = "";
			if (treeNode.isParent) {
				id = treeNode.id;
				id = getAllChildrenNodes(treeObj, treeNode, id);
				treeObj.removeNode(node);
			} else {
				id = treeNode.id;
				treeObj.removeNode(node);
			}
			// alert(id);
			$.ajax({
				type : "POST",
				url : "deleteNode.action?id=" + id,
				data : "",
				async : false,
				cache : false,
				success : function(msg) {
					// alert(msg);
				},
				error : function(e) {
					alert("对不起，后台处理发生异常，请联系管理员!");
				}
			});
		});
	}
	var addFolderBtn = $("#addFolderBtn_" + treeNode.tId);
	if (addFolderBtn) {
		addFolderBtn.bind("click", function() {
			// alert("点击了添加文件夹按钮的方法");
			// alert("属性值ID:" + this.id);
			var zTreeObj = $.fn.zTree.getZTreeObj("zTree");
			var name = "请输入新文件夹名称";
			var parentId = treeNode.id;
			var isParent = true;
			var caseType = "WEBUI";
			var id = "";
			// if (!treeNode.isParent) {
			// var parentId = treeNode.getParentNode().id;
			// }
			$.ajax({
				type : "POST",
				url : "addNode.action?parentId=" + parentId + "&name=" + name
						+ "&isParent=" + isParent + "&caseType=" + caseType,
				data : "",
				async : false,
				cache : false,
				success : function(msg) {
					id = msg;
				},
				error : function(e) {
					alert("对不起，后台处理发生异常，请联系管理员!");
				}
			});

			var newNode = {
				id : id,
				name : name,
				isParent : isParent
			};
			if (!treeNode.isParent) {
				newNode = zTreeObj.addNodes(treeNode.getParentNode(), newNode);
			} else {
				newNode = zTreeObj.addNodes(treeNode, newNode);
			}
			// alert("新节点信息:" + JSON.stringify(newNode));
			$.each(newNode, function(n, value) {
				// alert(n + ":" + value.tId);
				zTreeObj.editName(zTreeObj.getNodeByTId(value.tId));
			});
		});
	}
};
function removeHoverDom(treeId, treeNode) {
	$("#addBtn_" + treeNode.tId).unbind().remove();
	$("#removeBtn_" + treeNode.tId).unbind().remove();
	$("#editBtn_" + treeNode.tId).unbind().remove();
	$("#addFolderBtn_" + treeNode.tId).unbind().remove();
};
function zTreeOnRename(event, treeId, treeNode, isCancel) {
	var treeObj = $.fn.zTree.getZTreeObj(treeId);
	if (treeNode.name.length < 1) {
		treeObj.cancelEditName();
		return;
	}
	$.ajax({
		type : "POST",
		url : "updateNode.action?id=" + treeNode.id + "&name=" + treeNode.name,
		data : "",
		async : false,
		cache : false,
		success : function(msg) {
			// alert(msg);
			treeObj.setting.callback.onClick(null, treeId, treeNode);// 调用事件
		},
		error : function(e) {
			alert("对不起，后台处理发生异常，请联系管理员!");
		}
	});
}

function navigation(treeNode, str) {
	if (treeNode.id == 0) {
		str = "<li class=\"active\">" + treeNode.name + "</li>" + str;
		str = "<li><em class=\"glyphicon glyphicon-home\"></em></li>" + str;
		return str;
	} else {
		// alert(treeNode.isParent);
		if (treeNode.isParent) {
			str = "<li class=\"active\">" + treeNode.name + "</li>" + str;
		} else {
			str = "<li class=\"active\">" + treeNode.name + "(interface-"
					+ treeNode.id + ")" + "</li>" + str;
		}
		return navigation(treeNode.getParentNode(), str);
	}
}

function onClick(event, treeId, treeNode, clickFlag) {
	if (treeNode.isParent) {
		return;
	}
	$.ajax({
		url : "getWebuiCaseJson.action",
		type : "POST",
		data : "caseId=" + treeNode.id,
		dataType : "json",
//		async : false,
//		cache : false,
		beforeSend : function() {
			var target = $("#firstDiv").get(0);
			spinner.spin(target);
		},
		success : function(json) {

			spinner.spin();

			$("#pageNavigation").empty();

			var result = "";
			result = navigation(treeNode, result);
			$("#pageNavigation").prepend(result);

			var wc = jQuery.parseJSON(json.webuiCasesJson);
			$("#webui_step").attr("rows", 3);
			$("#webui_content").val("");
			$("#webui_step").val("");
			
			if (wc != null) {
				var sizeRow = wc.stepContent.split("\n").length;
				$("#webui_step").attr("rows", sizeRow);
				$("#webui_step").val(wc.stepContent);
				$("#webui_content").val(wc.codeContent);
			}
			$("#webui_result").val("");
		},
		error : function() {
			spinner.spin();
			alert("获取数据出现错误,请联系管理员!");
		}
	});
}

function getAllChildrenNodes(treeObj, treeNode, result) {
	if (treeNode.isParent) {
		var childrenNodes = treeNode.children;
		if (childrenNodes) {
			for (var i = 0; i < childrenNodes.length; i++) {
				if (childrenNodes[i].isParent) {
					result += ',' + childrenNodes[i].id;
					result = getAllChildrenNodes(treeObj, childrenNodes[i],
							result);
				} else {
					result += ',' + childrenNodes[i].id;
				}
			}
		}
	}
	return result;
}
var setting = {
	check : {
		enable : false
	},
	view : {
		addHoverDom : addHoverDom,
		removeHoverDom : removeHoverDom,
		dblClickExpand : true,
		showLine : true,
		selectedMulti : false
	},
	data : {
		simpleData : {
			enable : true
		// idKey: "id",
		// pIdKey: "pId",
		// rootPId: 0
		},
		keep : {
			leaf : false,
			parent : true
		}
	},
	callback : {
		onRename : zTreeOnRename,
		onClick : onClick,
	// asyncSuccess: zTreeOnAsyncSuccess
	},
	edit : {
		editNameSelectAll : true
	}
};

$(document)
		.ready(
				function() {
					$.ajax({
						url : "getWebuiTreeNode.action",
						type : "get",
						dataType : "json",
						success : initZtree
					});

					$("#webui_save").click(function() {
						var data = $("#webuiForm").serialize();
						var zTree = $.fn.zTree.getZTreeObj("zTree");
						var nodes = zTree.getSelectedNodes();
						data = data + "&caseId=" + nodes[0].id;
						$.ajax({
							type : "POST",
							url : "webuiCaseSave.action",
							data : data,
							// async : false,
							// cache : false,
							beforeSend : function() {
								var target = $("#firstDiv").get(0);
								spinner1.spin(target);
							},
							success : function(msg) {

								spinner1.spin();
								// alert(msg);
							},
							error : function(e) {
								spinner1.spin();
								alert("对不起，后台处理发生异常，请联系管理员!");
							}
						});
					});

					$("#webui_run")
							.click(
									function() {
										var data = $("#webuiForm").serialize();
										var zTree = $.fn.zTree
												.getZTreeObj("zTree");
										var nodes = zTree.getSelectedNodes();
										data = data + "&caseId=" + nodes[0].id+"&driverStr="+$("#driverChoose").val();
										$
												.ajax({
													type : "POST",
													url : "webuiCaseRun.action",
													data : data,
													// async : false,
													// cache : false,
													beforeSend : function() {
														var target = $(
																"#firstDiv")
																.get(0);
														spinner1.spin(target);
													},
													success : function(msg) {
														spinner1.spin();
														$("#webui_result").val(msg);
														
													},
													error : function(e) {
														spinner1.spin();
														alert("对不起，后台处理发生异常，请联系管理员!");
													}
												});
									});

				});

function initZtree(json) {
	var data = (json.caseWebuiJson);
	var zNodes = eval("(" + data + ")");
	zTreeObj = $.fn.zTree.init($('#zTree'), setting, zNodes);
}

function submitCheckedNodes(treeNode) {
	var nodes = new Array();
	// 取得选中的结点
	nodes = zTreeObj.getCheckedNodes(true);
	var str = "";
	for (i = 0; i < nodes.length; i++) {
		if (str != "") {
			str += ",";
		}
		str += nodes[i].id;
	}
	alert(str);
}